﻿using d1Common;
using d1Common.Interface;
using d1Common.WebController;
using d1Project.Modules.Common.doStorage.Domain;
using d1Project.Tools;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;

namespace d1Project.Modules.Common.doStorage.Services
{
    public class doStorageServiceQiniu : doStorageServiceBase, IDoStorage
    {
       
        private string DNS = null;
        public doStorageServiceQiniu()
        {
            this.DNS = doServices.Instance.DoConfig.readConfigStringValue(doStorageModule.ModuleIdDefine, "Qiniu", "DNS", null);
            if (this.DNS == null)
            {
                throw new Exception("配置文件中未定义DNS");
            }
        }

        public string getAccessUrl(string _path)
        {
            return this.DNS + _path;
        }

        public byte[] readFile(string _path)
        {
            byte[] _data = null;
            string _fileFullPath = this.DNS+_path;
            using (System.Net.WebClient wc = new System.Net.WebClient())
            {
                _data = wc.DownloadData(_fileFullPath);
            }
            return _data;
        }

        //写入七牛服务
        public string writeFile(string _moduleId, string _path, byte[] _content)
        {
            string _fileFullPath = _moduleId + "/" + _path;
            var result= QiniuMgr.Instance.uploadByte(_fileFullPath, _content);
            if (result.Code != 200) throw new UserException("上传文件异常,code=" + result.Code + "test=" + result.Text);
            return _fileFullPath;
              
        }
       
    }
}